package myjava3d;

import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.Material;
import javax.media.j3d.Texture2D;
import javax.media.j3d.TextureAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Color4f;
import javax.vecmath.Vector3f;

import start.Start;

import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.image.TextureLoader;

public class R2D2 extends BranchGroup {	
	public R2D2() {
		//Farben und Oberflaeche
		Color3f col=new Color3f(0.5f,0.5f,0.5f);				// Farbe R,G,B
	    
	    Color3f white=new Color3f(1.0f,1.0f,1.0f);	
	    Appearance ap_white = new Appearance();					// redApp - Aussehen
	    ap_white.setMaterial(new Material(white,white,white,white,100.0f));// Material 
	    ColoringAttributes ca_white=new ColoringAttributes();			// Farbattribute
	    ca_white.setShadeModel(ColoringAttributes.SHADE_GOURAUD);		// Schattierungs-Modell wird gesetzt
	    ap_white.setColoringAttributes(ca_white);	    
	    
	   //Erzeugung des Objekts: Die Lok
	    Appearance metall = this.texture("metall.jpg",480,312);
	    metall.setMaterial(new Material(white,white,white,col,150.0f));
		
		/*
		Color3f redCol = new Color3f(0.7f, 0.0f, 0.0f); //Eine Farbe nach RGB-Shema festlegen
		Color3f greenCol = new Color3f(0.0f, 0.7f, 0.0f); //Eine Farbe nach RGB-Shema festlegen
		Color3f blueCol = new Color3f(0.0f, 0.0f, 0.7f); //Eine Farbe nach RGB-Shema festlegen
		
		Appearance redApp = new Appearance(); //Neues Aussehen anlegen
		redApp.setMaterial(new Material(redCol, redCol, redCol, redCol, 50.0f)); //Material des Objekts festlegen
		Appearance greenApp = new Appearance(); //Neues Aussehen anlegen
		greenApp.setMaterial(new Material(greenCol, greenCol, greenCol, greenCol, 50.0f)); //Material des Objekts festlegen
		Appearance blueApp = new Appearance(); //Neues Aussehen anlegen
		blueApp.setMaterial(new Material(blueCol, blueCol, blueCol, blueCol, 50.0f)); //Material des Objekts festlegen
		
		
		ColoringAttributes ca = new ColoringAttributes(); //Farbattribut anlegen
		ca.setShadeModel(ColoringAttributes.SHADE_GOURAUD); //Flat-Shading fuer Farbeigenschaften festlegen
		redApp.setColoringAttributes(ca); //Farbattribute auf Material legen
		greenApp.setColoringAttributes(ca); //Farbattribute auf Material legen
		blueApp.setColoringAttributes(ca); //Farbattribute auf Material legen
		*/
		
		//Physikalische Objekte erzeugen
		Cylinder koerper = new Cylinder(0.2f, 0.4f, Cylinder.GENERATE_TEXTURE_COORDS + Cylinder.GENERATE_NORMALS, metall);
		Sphere kopf = new Sphere(koerper.getRadius(), Sphere.GENERATE_TEXTURE_COORDS + Sphere.GENERATE_NORMALS, metall);
		Cylinder unterteil = new Cylinder(0.15f, 0.025f, Cylinder.GENERATE_TEXTURE_COORDS + Cylinder.GENERATE_NORMALS, metall);
		Cylinder armkugelLinks = new Cylinder(0.1f, 0.05f, Cylinder.GENERATE_TEXTURE_COORDS + Cylinder.GENERATE_NORMALS, metall);
		Cylinder armkugelRechts = new Cylinder(0.1f, 0.05f, Cylinder.GENERATE_TEXTURE_COORDS + Cylinder.GENERATE_NORMALS, metall);
		Box armLinks = new Box(0.03f, 0.16f, 0.04f, Box.GENERATE_TEXTURE_COORDS + Box.GENERATE_NORMALS, metall);
		Box armRechts = new Box(0.03f, 0.16f, 0.04f, Box.GENERATE_TEXTURE_COORDS + Box.GENERATE_NORMALS, metall);
		Box fuss = new Box(0.05f, 0.05f, 0.05f, Box.GENERATE_TEXTURE_COORDS + Box.GENERATE_NORMALS, metall);
		
		//Bezeichnungen fuers Picking
		koerper.setUserData("koerper");
		kopf.setUserData("kopf");
		unterteil.setUserData("unterteil");
		armkugelLinks.setUserData("armkugelLinks");
		armkugelRechts.setUserData("armkugelRechts");
		armLinks.setUserData("armLinks");
		armRechts.setUserData("armRechts");
		fuss.setUserData("fuss");
		
		
		
		//Transformationen
		//Kopf
		Transform3D transKopf = new Transform3D();
		transKopf.setTranslation(new Vector3f(0.0f, (koerper.getHeight()/2), 0.0f));
		TransformGroup transKopfGroup = new TransformGroup(transKopf);
		transKopfGroup.addChild(kopf);
		
		//Unterteil
		Transform3D transUnterteil = new Transform3D();
		transUnterteil.setTranslation(new Vector3f(0.0f, -(koerper.getHeight()/2)-(unterteil.getHeight()/2), 0.0f));
		TransformGroup transUnterteilGroup = new TransformGroup(transUnterteil);
		transUnterteilGroup.addChild(unterteil);
		
		//Armkugel Links
		Transform3D transArmkugelLinks = new Transform3D();
		transArmkugelLinks.rotZ(Math.PI/2);
		transArmkugelLinks.setTranslation(new Vector3f(koerper.getRadius()+(armkugelLinks.getHeight()/2), 0.1f, 0.0f));
		TransformGroup transArmkugelLinksGroup = new TransformGroup(transArmkugelLinks);
		transArmkugelLinksGroup.addChild(armkugelLinks);
		
		//Armkugel Rechts
		Transform3D transArmkugelRechts = new Transform3D();
		transArmkugelRechts.rotZ(Math.PI/2);
		transArmkugelRechts.setTranslation(new Vector3f(-koerper.getRadius()-(armkugelRechts.getHeight()/2), 0.1f, 0.0f));
		TransformGroup transArmkugelRechtsGroup = new TransformGroup(transArmkugelRechts);
		transArmkugelRechtsGroup.addChild(armkugelRechts);
		
		//Unterteil
		Transform3D transFuss = new Transform3D();
		transFuss.setTranslation(new Vector3f(0.0f, -(koerper.getHeight()/2)-(unterteil.getHeight()/2)-fuss.getXdimension(), 0.0f));
		TransformGroup transFussGroup = new TransformGroup(transFuss);
		transFussGroup.addChild(fuss);
		
		//Arm Links
		Transform3D transArmLinks = new Transform3D();
		transArmLinks.setTranslation(new Vector3f((koerper.getRadius())+armLinks.getXdimension(), -0.15f, 0.0f));
		TransformGroup transArmLinksGroup = new TransformGroup(transArmLinks);
		transArmLinksGroup.addChild(armLinks);
		
		//Arm Rechts
		Transform3D transArmRechts = new Transform3D();
		transArmRechts.setTranslation(new Vector3f(-(koerper.getRadius())-armLinks.getXdimension(), -0.15f, 0.0f));
		TransformGroup transArmRechtsGroup = new TransformGroup(transArmRechts);
		transArmRechtsGroup.addChild(armRechts);
		
		
		
		//In Branchgroup packen
		//branchGroup.addChild(box);
		addChild(koerper);
	    addChild(transKopfGroup);
	    addChild(transUnterteilGroup);
	    addChild(transArmkugelLinksGroup);
	    addChild(transArmkugelRechtsGroup);
	    addChild(transArmLinksGroup);
	    addChild(transArmRechtsGroup);
	    addChild(transFussGroup);
		//addChild(background);
	}
	
	public Appearance texture(String s, int breite, int hoehe) {
		TextureLoader loader = new TextureLoader(s, Start.getMyObject());
        ImageComponent2D image = loader.getImage();
        Texture2D texture = new Texture2D();
        int form  = texture.getFormat();
        int base = texture.getMipMapMode();
        texture = new Texture2D(base,form,breite,hoehe);
        texture.setImage(0, image);
        Appearance appear = new Appearance();
        appear.setTexture(texture);
        TextureAttributes texat = new TextureAttributes( 
        		TextureAttributes.COMBINE_INTERPOLATE,new Transform3D(),
        		new Color4f(),
        		TextureAttributes.NICEST );
        appear.setTextureAttributes(texat);
        appear.setMaterial(new Material());
        return(appear);
	}
}
